package dao.daoImpl;

import dao.TeaDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import pojo.PageInfo;
import pojo.Student;
import pojo.Teacher;
import utils.JDBCUtils;

import java.sql.SQLException;
import java.util.List;

public class TeaDaoImpl implements TeaDao {


    @Override
    public List<Student> findStusById(String id) throws SQLException {
        String sql ="select * from student where stu_id in (select stu_id from stu_tea where stu_tea.`tea_id` = ?)";
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        List<Student> list = queryRunner.query(sql,new BeanListHandler<Student>(Student.class),id);
        return  list;
    }

    @Override
    public List<Student> findStusByIdIncludePage(String id, PageInfo pageInfo) throws SQLException {
        String sql ="select * from student where stu_id in (select stu_id from stu_tea where stu_tea.`tea_id` = ?) limit ?,?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        List<Student> list = queryRunner.query(sql,new BeanListHandler<Student>(Student.class),id,pageInfo.getStart(),pageInfo.getRows());
        return  list;
    }

    @Override
    public Teacher findTeaById(String id) throws SQLException {

        String sql =" SELECT * FROM teacher WHERE tea_id = ?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
        Teacher tea =  queryRunner.query(sql, new BeanHandler<Teacher>(Teacher.class),id);

        return tea;
    }
}
